package com.sitech.openapi.service;

import com.sitech.openapi.domain.customer.Cust;
import com.sitech.openapi.domain.user.*;
import com.sitech.openapi.mapper.*;
import com.sitech.openapi.utils.DateUtil;
import com.sitech.openapi.utils.NumberUtil;
import com.sitech.openapi.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yinjj on 2019/2/14.
 * 【集团派发单，请安排】关于随选宽带销售品实例上传至集团集约系统的工作通知
 */
@Service
@Slf4j
public class ProdOfferInstInfoServiceImpl implements ProdOfferInstInfoService{

    @Autowired
    private ProdOfferInstAttrMapper prodOfferInstAttrMapper;
    @Autowired
    private ProdInstAccNbrMapper prodInstAccNbrMapper;
    @Autowired
    private ProdOfferInstMapper prodOfferInstMapper;
    @Autowired
    private OfferProdInstRelMapper offerProdInstRelMapper;
    @Autowired
    private ProdInstMapper prodInstMapper;
    @Autowired
    private ProdOfferMapper prodOfferMapper;
    @Autowired
    private ProdOfferAssortMapper prodOfferAssortMapper;
    @Autowired
    private ProdOfferInstInfoMapperService ProdOfferInstInfoMapperService;
    @Autowired
    private CustMapper custMapper;

    public List qryKDSXOfferInfo(Map<String, Object> paraMap) {
        String accNum = StringUtil.obj2Str(paraMap.get("accNum"));
        String account = StringUtil.obj2Str(paraMap.get("account"));
        List retList = new ArrayList<>();
        List<Map<String,String>> accInfoList = new ArrayList<>();
        Boolean isSXFlag = false;
        if(StringUtil.isNotEmptyOrNull(accNum)){
            Map<String,String> accMap = new HashMap<>();
            accMap.put("accNbrTypeCd","4");
            accMap.put("accNbr",accNum);
            accInfoList.add(accMap);
            //入参是c网号码,通过c网号码查询对应的宽带号码
            List<String> accountList = ProdOfferInstInfoMapperService.queryKDNbrByAccNbr(accNum);
            if(accountList!=null && accountList.size()>0){
                account = StringUtil.obj2Str(accountList.get(0));
                Map<String,String> accountMap = new HashMap<>();
                accountMap.put("accNbrTypeCd","2");
                accountMap.put("accNbr",account);
                accInfoList.add(accountMap);
            }
        }else if(StringUtil.isNotEmptyOrNull(account)){
            Map<String,String> accountMap = new HashMap<>();
            accountMap.put("accNbrTypeCd","2");
            accountMap.put("accNbr",account);
            accInfoList.add(accountMap);
        }

        if(accInfoList!=null && accInfoList.size() > 0){
            for(Map<String,String> retMap : accInfoList){
                String accNbr = StringUtil.obj2Str(retMap.get("accNbr"));
                String accNbrTypeCd = StringUtil.obj2Str(retMap.get("accNbrTypeCd"));
                //入参是宽带账号,根据号码、号码类型查询产品实例id
                ProdInstAccNbrExample prodInstAccNbrExample = new ProdInstAccNbrExample();
                prodInstAccNbrExample.createCriteria().andAccNbrEqualTo(accNbr).andAccNbrTypeCdEqualTo(Integer.parseInt(accNbrTypeCd));
                List<ProdInstAccNbr> prodInstAccNbrs = prodInstAccNbrMapper.selectByExample(prodInstAccNbrExample);
                if(prodInstAccNbrs != null && prodInstAccNbrs.size() > 0){
                    //通过产品实例id查询客户信息
                    CProdInst cProdInst = prodInstMapper.selectByPrimaryKey(prodInstAccNbrs.get(0).getProdInstId());
                    if(cProdInst == null){
                        throw new RuntimeException("根据实例未查询到用户信息！");
                    }
                    Cust cust = custMapper.selectByPrimaryKey(cProdInst.getOwnerCustId());
                    if(cust == null){
                        throw new RuntimeException("根据实例未查询到客户信息！");
                    }
                    //通过产品实例id查询销售品实例信息
                    List<ProdOfferInst> prodOfferInsts = new ArrayList<>();
                    ProdOfferInstExample prodOfferInstExample = new ProdOfferInstExample();
                    prodOfferInstExample.createCriteria().andProdInstIdEqualTo(prodInstAccNbrs.get(0).getProdInstId());
                    prodOfferInsts = prodOfferInstMapper.selectByExample(prodOfferInstExample);
                    if (prodOfferInsts != null && prodOfferInsts.size() > 0) {
                        for (ProdOfferInst prodOfferInst : prodOfferInsts) {
                            OfferInst offerInst = new OfferInst();
                            offerInst.setOfferInstId(prodOfferInst.getProdOfferInstId());
                            offerInst.setExtOfferInstId(prodOfferInst.getExtProdOfferInstId());
                            ProdOffer prodOffer = new ProdOffer();
                            prodOffer = prodOfferMapper.selectByPrimaryKey(prodOfferInst.getProdOfferId());
                            if(StringUtil.isEqual(prodOffer.getOfferType(),"12")&&StringUtil.isEqual(accNbrTypeCd,"2")){
                                ProdOfferAssort prodOfferAssort = new ProdOfferAssort();
                                ProdOfferAssortExample prodOfferAssortExample = new ProdOfferAssortExample();
                                prodOfferAssortExample.createCriteria().andProdOfferIdEqualTo(prodOffer.getProdOfferId()).andOfferTypeCEqualTo(250);
                                List prodOfferAssortList = prodOfferAssortMapper.selectByExample(prodOfferAssortExample);
                                if(prodOfferAssortList!=null && prodOfferAssortList.size()>0){
                                    isSXFlag = true;
                                }else {
                                    continue;
                                }
                            }
                            offerInst.setOfferNbr(prodOffer.getOfferNbr());
                            offerInst.setOfferType(Long.parseLong(prodOffer.getOfferType()));
                            offerInst.setOffName(prodOffer.getProdOfferName());
                            offerInst.setOwnerCustId(cust.getCustId());
                            if(StringUtil.isNotEmptyOrNull(cust.getExtCustId())){
                                offerInst.setExtOwnerCustId(Long.parseLong(cust.getExtCustId()));
                            }
                            offerInst.setEffDate(DateUtil.toStringYmdHmsWthH(prodOfferInst.getEffDate()));
                            offerInst.setExpDate(DateUtil.toStringYmdHmsWthH(prodOfferInst.getExpDate()));
                            //offerInst.setOfferAgreeId();
                            offerInst.setCreateOrgId(prodOffer.getCreateRegion());
                            offerInst.setExpProcMethod(prodOffer.getExpProcMethod());
                            //offerInst.setEffType();// 生效方式
                            String commonRegionId = "8"+prodOfferInst.getLanId()+"00";
                            offerInst.setLanId(NumberUtil.parseLong(commonRegionId));
                            offerInst.setRegionId(NumberUtil.parseLong(commonRegionId));
                            offerInst.setStatusCd(prodOfferInst.getStatusCd());
                            offerInst.setBusiModDate(DateUtil.toStringYmdHmsWthH(prodOfferInst.getStatusDate()));
                            offerInst.setCreateDate(DateUtil.toStringYmdHmsWthH(prodOfferInst.getCreateDate()));
                            //offerInst.setRemark();
                            // 销售品属性实例
                            List<ProdOfferInstAttr> prodOfferInstAttrs = new ArrayList<>();
                            List<OfferInstAttr> offerInstAttrs = new ArrayList<>();
                            ProdOfferInstAttrExample prodOfferInstAttrExample = new ProdOfferInstAttrExample();
                            prodOfferInstAttrExample.createCriteria().andProdOfferInstIdEqualTo(prodOfferInst.getProdInstId());
                            prodOfferInstAttrs = prodOfferInstAttrMapper.selectByExample(prodOfferInstAttrExample);
                            if (prodOfferInstAttrs != null && prodOfferInstAttrs.size() > 0) {
                                for (ProdOfferInstAttr prodOfferInstAttr : prodOfferInstAttrs) {
                                    OfferInstAttr offerInstAttr = new OfferInstAttr();
                                    offerInstAttr.setAttrId(prodOfferInstAttr.getAttrId());
                                    offerInstAttr.setAttrValue(prodOfferInstAttr.getAttrValue());
                                    offerInstAttrs.add(offerInstAttr);

                                }
                            }
                            offerInst.setOfferInstAttrs(offerInstAttrs);
                            // 查询产品销售品实例关系
                            List<COfferProdInstRel> COfferProdInstRels = new ArrayList<>();
                            List<OfferProdInstRel> offerProdInstRels = new ArrayList<>();
                            OfferProdInstRelExample offerProdInstRelExample = new OfferProdInstRelExample();
                            offerProdInstRelExample.createCriteria().andProdInstIdEqualTo(prodOfferInst.getProdInstId());
                            COfferProdInstRels = offerProdInstRelMapper.selectByExample(offerProdInstRelExample);
                            if (COfferProdInstRels != null && COfferProdInstRels.size() > 0) {
                                for (COfferProdInstRel COfferProdInstRel : COfferProdInstRels) {
                                    OfferProdInstRel offerProdInstRel = new OfferProdInstRel();
                                    offerProdInstRel.setOfferProdInstRelId(COfferProdInstRel.getOfferProdInstRelId());
                                    //offerProdInstRel.setExtOfferProdInstRelId();
                                    offerProdInstRel.setOfferInstId(prodOfferInst.getProdOfferInstId());
                                    offerProdInstRel.setExtOfferInstId(prodOfferInst.getExtProdOfferInstId());
                                    offerProdInstRel.setProdInstId(COfferProdInstRel.getProdInstId());
                                    offerProdInstRel.setExtProdInstId(cProdInst.getExtProdInstId());
                                    offerProdInstRel.setRoleCd(StringUtil.obj2Str(COfferProdInstRel.getRoleCd()));
                                    //offerProdInstRel.setRoleName();
                                    //offerProdInstRel.setStatusCd();
                                    //offerProdInstRel.setRemark();
                                    offerProdInstRel.setAccNum(prodInstAccNbrs.get(0).getAccNbr());
                                    offerProdInstRel.setEffDate(DateUtil.toStringYmdHmsWthH(COfferProdInstRel.getEffDate()));
                                    offerProdInstRel.setExpDate(DateUtil.toStringYmdHmsWthH(COfferProdInstRel.getExpDate()));

                                    offerProdInstRels.add(offerProdInstRel);

                                }
                            }
                            offerInst.setOfferProdInstRels(offerProdInstRels);
                            retList.add(offerInst);
                        }
                    }
                }
            }

        }
        //如果不是宽带随选且返回值为空
        if(!isSXFlag&& retList.size() == 0){
            throw new RuntimeException("根据实例未查询到宽带随选信息！");
        }
        return retList;
    }
}
